package com.qipay.operate.service.impl;

import com.qipay.baas.model.OperateLog;
import com.qipay.mapper.OperateLogMapper;
import com.qipay.operate.service.OperateLogService;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Creator: Yao
 * Date:    2017/10/12
 * For:
 * Other:
 */
@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Autowired
    OperateLogMapper logMapper;

    @Override
    @Transactional
    public void save(OperateLog log) {

        // check
        Assert.notNull(log);

        // save
        logMapper.insert(log);
    }

    @Override
    public List<OperateLog> selectLogByUrlAndType(Integer uid, String url, int p, int pz) {
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        Map<String, Object> mapList = new HashMap<>(4);
        if (uid != null) {
            mapList.put("uid", uid);
        }
        if (StringUtils.isNotEmpty((url))) {
            mapList.put("url", url);
        }
        mapList.put("offset", (p - 1) * pz);
        mapList.put("size", pz);
        return logMapper.selectByUidAndType(mapList);
    }

    @Override
    public Long countLogByUrlAndType(Integer uid, String url) {
        Map<String, Object> mapList = new HashMap<>(4);
        if (uid != null) {
            mapList.put("uid", uid);
        }
        if (StringUtils.isNotEmpty((url))) {
            mapList.put("url", url);
        }
        return logMapper.countByUidAndType(mapList);
    }
}
